package com.petlab.forum;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.petlab.common.FilePath;
import com.petlab.model.Article;
import com.petlab.model.Comment;
import com.petlab.service.ArticleService;

@Component("commentDispatcher")
public class CommentDispatcherImpl implements ObjectSaveToXML {

	private ArticleService articleService;

	public ArticleService getArticleService() {
		return articleService;
	}

	@Resource
	public void setArticleService(ArticleService articleService) {
		this.articleService = articleService;
	}

	public void saveObjectToXMLFile(Object o) {
		// TODO Auto-generated method stub
		Comment comment = (Comment) o;
		int articleId = comment.getArticleId();

		Article article = articleService.getArticleById(articleId);
		String themeTitle = article.getThemeTitle();

		String filename = FilePath
				.getParentDirectory(FilePath.getParentDirectory(this.getClass()
						.getResource("/").getFile()))
				+ "forum/" + themeTitle + "/" + themeTitle + ".xml";

		AddDetailsToXMLFile.updateCommentNumbers(filename, String
				.valueOf(articleId));
	}
}
